package com.jiuyin.dianjing.ui.activity.match;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.EntryListModel;
import com.jiuyin.dianjing.model.UserItemModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.LogUtil;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPETITIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPET_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_MEMBER_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TACTICS;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;

/**
 * 赛事列表
 */
public class MatchApplyListActivity extends BaseActivity {
    @BindView(R.id.rcv_page)
    RecyclerView mRcvPage;
    private int mTactics;
    private String mCompetitionId;

    private BaseRecyclerAdapter<EntryListModel> mAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_match_apply_list;
    }

    @Override
    protected void initData() {
        mTactics = getIntent().getIntExtra(KEY_TACTICS, 0);
        mCompetitionId = getIntent().getStringExtra(KEY_COMPETITIONID);
        mAdapter = new BaseRecyclerAdapter<EntryListModel>(R.layout.item_team_apply_list) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, EntryListModel model, int position) {
                if (null != model) {
                    TextView textView = holder.itemView.findViewById(R.id.tv_chage);
                    textView.setVisibility(View.GONE);
                    holder.text(R.id.tv_name, model.getName());
//                    holder.itemView.findViewById(R.id.tv_name).setVisibility(View.GONE);
                    ImageView ivGame = holder.itemView.findViewById(R.id.iv_head);
                    Glide.with(MatchApplyListActivity.this)
                            .load(model.getLogo())
                            .override(DisplayUtil.dp2px(MatchApplyListActivity.this, 27), DisplayUtil.dp2px(MatchApplyListActivity.this, 27))
                            .into(ivGame);
                }
            }
        };
        DefaultItemDecoration itemDecoration = new DefaultItemDecoration(ContextCompat.getColor(this, R.color.color_list_divider));
        mRcvPage.addItemDecoration(itemDecoration);
        mRcvPage.setAdapter(mAdapter);
        fetchData();
    }

    @Override
    protected void initView() {
        String title = mTactics == 0 ? getString(R.string.match_competitor) : getString(R.string.match_competitor_team);
        setTitle(title);
    }

    private void fetchData() {
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_TACTICS, mTactics);
        data.put(KEY_COMPET_ID, mCompetitionId);
        JSONObject jsonObject = new JSONObject(data);
        ProgressDialogCallback progress = new ProgressDialogCallback() {
            @Override
            public void showProgressDialog() {
                showLoading();
            }

            @Override
            public void dismissProgressDialog() {
                dismissLoading();
            }
        };
        ApiResultCallback request = new ApiResultCallback() {
            @Override
            public void onSuccess(JsonObject data) {
                if (null != data && data.has(KEY_MEMBER_LIST)) {
                    List<EntryListModel> userItemModels = new Gson().fromJson(data.getAsJsonArray(KEY_MEMBER_LIST), new TypeToken<List<EntryListModel>>() {
                    }.getType());
                    if (null != userItemModels) {
                        int size = userItemModels.size();
                        if (0 != size) {
                            onRefresh(userItemModels);
                        }
                    }
                }
            }

            @Override
            public void onFail(String msg) {

            }

            @Override
            public void onError(String msg) {
            }
        };
        ServerApi.post(ApiEnum.APP_GET_COMPET_MEMBER_LIST.getUrl(), jsonObject, null,
                progress, d -> addDisposable(d), request);
    }

    private void onRefresh(List<EntryListModel> models) {
        mAdapter.refresh(models);
    }
}
